import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-13
 * Time: 21:09
 */
public class Demo31 {
    public static int flag = 1;
    public static void main(String[] args) {
        //线程1进行读取
        Thread t1 = new Thread(() -> {
            while(flag == 1) {
                try {
                    //优不优化不重要了，能够解决内存可见性问题，但是会影响到程序的效率-->引入volatile
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t1线程结束");
        });
        //线程2进行修改
        Thread t2 = new Thread(()-> {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入flag的值：");
            flag = sc.nextInt();
        });
        t1.start();
        t2.start();
    }
}
